<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <meta charset="UTF-8">
    <title>扫描到货验收单</title>

    <script src="{% static 'front/jquery-3.6.0.min.js' %}"></script>
</head>

<body onresize="hero();" style="background-image: url('{% static 'front/back.png' %}');background-size: cover;">

  <object id="ScanerOCX"
          hidden="1"
    name="ScanerOCX"
    classid="CLSID:49903B72-9F44-41E1-A79B-B85A8BCEB89A"
    >
  </object>

  <!-- <div class="tip">
    请将到货验收单按首页在前的顺序放入扫描仪，然后点击“扫描单据并上传”，等待页面提示上传成功。
  </div> -->
    
  <div style="margin-left: 38%; margin-top: 37%">
    <input id="BTUpload" type="button" value="&nbsp;扫描单据并上传&nbsp;" onclick="return BTUpload_onclick()" />
  </div>

{#  <div class="form_upload" style="position: absolute; top: 500px;left: 550px;">#}
{#      <form id="form_upload" method="POST" enctype="multipart/form-data" hidden="">#}
{#      <!-- <input type="text" id="doc_mark" name="doc_mark" hidden=""> -->#}
{#          <input id="in_upload" type="file" name="img_upload" multiple="multiple" accept="image/*"#}
{##}
{#          >#}
{#      </form>#}
{#  </div>#}

  <script language="javascript" type="text/javascript">

    //================================
    var id = "{{ server }}";
    var port = "{{ port }}";
    var sub_url = "{% url 'receipt:scan_pdf' %}" + "?mark={{ mark }}";
    //=================================

    var obj;

    window.onload=function() {
        hero();

        obj = document.getElementById("ScanerOCX"); 
        obj.DSMode(9);
        let res = obj.openScanner();
        if (res != 1) {
          let err = obj.GetErrorCode();
          alert("扫描仪设备初始化失败!错误:" + String(err));
        }
        //alert(res);
    }

    window.onbeforeunload=function () {
        obj.CloseScanner();
    }

    function BTUpload_onclick() {

      obj.Brightness = -500;
      obj.AutoCrop = 1;
      obj.AutoDesk = 1;
      obj.ShowUI = 0;
      obj.AutoScan = 0;
      obj.Attribute = 2;
      obj.ScanResolution = 300;  // DPI
      obj.ScanPixelType = 2;     // 0 bw 1 gray 2 RGB
      obj.ImageFormat = 1;       // 0 bmp 1 jpg 2 tif 3 multi tif 4 pdf 5 multi pdf
      obj.CompressionRate = 90;  // 图像质量
      obj.Contrast = 0;          // 对比度
      obj.AutoDiscardBlankPages = -1;
      obj.TiffCompressType = 1;
      obj.Rotation = 0;
      obj.SetBlankPageFindSensitivity(1);

      let path = "c:\\scan_temp";
      obj.SetImageName(path, "scan", 1, 2);

      let fso = new ActiveXObject("Scripting.FileSystemObject");  
      if (fso.FolderExists(path)) {  
        try {
            fso.DeleteFile(path + "\\*");
        } catch(e) { 
            alert(e.message);
        }                 
      }

      let res = obj.Scan(-1, 0);
      if (res != 1) {alert("扫描仪扫描失败!")}

      let CurImgPath = path + "\\" + getFormatDate() + ".pdf"; 
      res = obj.BookImg2PDF(path, "jpg", CurImgPath);
      if(res != 1) {alert("扫描仪保存失败!");}

      let upres = obj.UpImageFileExs(id, port, sub_url, CurImgPath, 1);
      let response = obj.GetHttpResponse();
      if (upres == 0 && response != "") {
        alert("扫描件上传成功!");
        window.opener = null;
        window.close();
      }

    }
    
    function getFormatDate() {
      var nowDate = new Date();
      var year = nowDate.getFullYear();
      var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
      var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
      var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
      var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
      var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
      return year + "" + month + "" + date+""+hour+""+minute+""+second;
    }

  </script>

  <script type="text/javascript">
    function hero() {
        let herowidth = 700;
        let heroheight = 400;
        window.resizeTo(herowidth, heroheight);
    }
  </script>

  <style type="text/css">
    input {
        font-weight: bold;
        font-size: 24px;
    }
    .tip {
      text-align: center; 
      font-size: 16px; 
      color: blue;
    }
  </style>

</body>
</html>